<template>
  <el-dialog :visible.sync="dialogVisible" @close="close">
    <div slot="title">{{ info.type === 1 ? "新增" : "编辑" }}</div>
    <el-form
      hide-required-asterisk
      :model="refFormData"
      label-width="80px"
      :rules="rules"
      class="label-row"
      ref="refForm"
    >
      <el-form-item prop="name" label="主体名称">
        <el-input
          clearable
          size="small"
          autofocus
          v-model="refFormData.name"
          placeholder="主体名称"
        ></el-input>
      </el-form-item>
      <el-form-item prop="domain" label="域名">
        <el-input
          clearable
          size="small"
          autofocus
          v-model="refFormData.domain"
          placeholder="域名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="status" label="状态">
        <el-radio-group v-model="refFormData.status">
          <el-radio
            :label="item.id"
            v-for="item in statusList"
            :key="item.id"
            >{{ item.name }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="warning" @click="close()">取消</el-button>
      <el-button type="success" @click="submit('refForm')">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { statusList } from "../config/index";

export default {
  name: "dialogBox",
  props: {
    info: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      statusList,
      dialogVisible: true, // 弹框
      refFormData: {
        name: '',
        domain: '',
        status: 1,
      },
      rules: {
        // name: [
        //   { required: true, message: "", trigger: "blur" },
        //   { min: 1, max: 30, message: "", trigger: "blur" },
        // ],
      },
    };
  },
  mounted() {
    this.refFormData = {
      ...this.info,
    };
  },
  methods: {
    /**
     * @Desc 数据提交
     * */
    async submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return;
        const params = {
          ...this.refFormData,
        };
        const { data } = await this.$api.domain.dimainOperate(
          {
             [this.info.type === 1 ? "create" : "update"]: params
          });
        this.__globalHandleResponseMixin(data, () => {
          this.$emit("getList");
          this.close();
        });
      });
    },

    /**
     * @Desc 关闭
     * @params {String}
     * @return {Object}
     * */
    close() {
      this.$emit("close");
    },
  },
};
</script>

<style lang='less' scoped>
</style>
